﻿using Ecommerce.Domain;
using Ecommerce.Domain.Dto;
using Ecommerce.Domain.Holiday;
using Ecommerce.Domain.User;
using Ecommerce.Repository;
using Ecommerce.Repository.Impl.Leaves;
using Ecommerce.Repository.Interface.Leaves;
using Ecommerce.Repository.Interface.User;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Numerics;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;

namespace Ecommerce.Services.Leaves
{
    public class LeaveServices : ILeaveServices
    {
        private readonly IParmentRepository parmentRepository;

        private readonly ILeaveRepository leaveRepository;

       private readonly IAccountRepository accountRepository;

        public LeaveServices(IParmentRepository parmentRepository, ILeaveRepository leaveRepository, IAccountRepository accountRepository)
        {
            this.parmentRepository = parmentRepository;
            this.leaveRepository = leaveRepository;
            this.accountRepository = accountRepository;
        }

        public Task<int> Addleave(Leave leave)
        {
            return leaveRepository.AddAsync(leave);
        }

        public async Task<List<LeacPart>> GetLeaves(string Bname)
        {
            var list = from a in await leaveRepository.GetAllAsync()
                       join b in await parmentRepository.GetAllAsync() on a.Pid equals b.Pid
                       select new LeacPart
                       {
                           Pid = a.Pid,
                           Lid = a.Lid,
                           Houes = a.Houes,
                           Qtime = a.Qtime,
                           Btime = a.Btime,
                           State = a.State,
                           QWhy = a.QWhy,
                           BName = a.BName,
                           DTime = a.DTime,
                           LType = a.LType,
                           Lname = a.Lname,
                           Pname = b.Pname
                       };
            return list.Where(w=>w.BName== Bname || w.Lname==Bname).ToList();
        }

        public async Task<List<LeacPart>> GetLeavesId(int Lid)
        {
            var list = from a in await leaveRepository.GetAllAsync()
                       join b in await parmentRepository.GetAllAsync() on a.Pid equals b.Pid where a.Lid == Lid
                       select new LeacPart 
                       {
                           Pid = a.Pid,
                           Lid = a.Lid,
                           Houes = a.Houes,
                           Qtime = a.Qtime,
                           Btime = a.Btime,
                           State = a.State,
                           QWhy = a.QWhy,
                           BName = a.BName,
                           DTime = a.DTime,
                           LType = a.LType,
                           Lname = a.Lname,
                           Pname = b.Pname
                       };
            return list.ToList();
        }

        public Task<List<Partment>> GetPartments()
        {
            return parmentRepository.GetAllAsync();
        }

        public Task<List<Account>> GetRoles()
        {
            return accountRepository.GetAllAsync();
        }

        public async Task<int> leaveUpdate(int Lid,bool State)
        {
            Leave leave = new Leave();
            var list = await leaveRepository.GetAsync(w=>w.Lid==leave.Lid);
            leave.Lid = list.FirstOrDefault().Lid;
            leave.Lname = list.FirstOrDefault().Lname;
            leave.Qtime = list.FirstOrDefault().Qtime;
            leave.State=State;
            leave.Btime= list.FirstOrDefault().Btime;
            leave.BName = list.FirstOrDefault().BName;
            leave.Pid = list.FirstOrDefault().Pid;
            leave.LType= list.FirstOrDefault().LType;
            leave.DTime = list.FirstOrDefault().DTime;
            leave.QWhy = list.FirstOrDefault().QWhy;
            leave.Houes = list.FirstOrDefault().Houes;
            return await leaveRepository.Update(leave);    
        }
    }
}
